<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="流程id" prop="wid">
        <el-input
          v-model="queryParams.wid"
          placeholder="请输入流程id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="父节点" prop="pid">
        <el-input
          v-model="queryParams.pid"
          placeholder="请输入父节点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经办类型" prop="otype">
        <el-select v-model="queryParams.otype" placeholder="请选择经办类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.handle_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否转办" prop="zflag">
        <el-select v-model="queryParams.zflag" placeholder="请选择是否转办" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="转办类型" prop="ttype">
        <el-select v-model="queryParams.ttype" placeholder="请选择转办类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.handle_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="起点标识" prop="sflag">
        <el-input
          v-model="queryParams.sflag"
          placeholder="请输入起点标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="完结标识" prop="eflag">
        <el-select v-model="queryParams.eflag" placeholder="请选择完结标识" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.glo_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="节点编号" prop="nodeno">
        <el-input
          v-model="queryParams.nodeno"
          placeholder="请输入节点编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程编号" prop="nids">
        <el-input
          v-model="queryParams.nids"
          placeholder="请输入流程编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:sysNode:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="sysNodeList"
      row-key="nid"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="流程id" prop="wid" />
      <el-table-column label="节点条件" align="center" prop="cond" />
      <el-table-column label="父节点" align="center" prop="pid" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="经办类型" align="center" prop="otype">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.handle_type" :value="scope.row.otype"/>
        </template>
      </el-table-column>
      <el-table-column label="是否可以转办" align="center" prop="zflag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.zflag"/>
        </template>
      </el-table-column>
      <el-table-column label="转办类型" align="center" prop="ttype">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.handle_type" :value="scope.row.ttype"/>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="起点标识" align="center" prop="sflag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.sflag"/>
        </template>
      </el-table-column>
      <el-table-column label="完结标识" align="center" prop="eflag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.eflag"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.glo_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="节点编号" align="center" prop="nodeno" />
      <el-table-column label="流程编号" align="center" prop="nids" />
      <el-table-column label="描述" align="center" prop="memo" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:sysNode:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:sysNode:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:sysNode:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改工作流节点对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="流程id" prop="wid">
          <el-input v-model="form.wid" placeholder="请输入流程id" />
        </el-form-item>
        <el-form-item label="节点条件" prop="cond">
          <el-input v-model="form.cond" placeholder="请输入节点条件" />
        </el-form-item>
        <el-form-item label="父节点" prop="pid">
          <treeselect v-model="form.pid" :options="sysNodeOptions" :normalizer="normalizer" placeholder="请选择父节点" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="经办类型" prop="otype">
          <el-select v-model="form.otype" placeholder="请选择经办类型">
            <el-option
              v-for="dict in dict.type.handle_type"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否可以转办">
          <el-radio-group v-model="form.zflag">
            <el-radio
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
:label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="转办类型" prop="ttype">
          <el-select v-model="form.ttype" placeholder="请选择转办类型">
            <el-option
              v-for="dict in dict.type.handle_type"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="起点标识" prop="sflag">
          <el-input v-model="form.sflag" placeholder="请输入起点标识" />
        </el-form-item>
        <el-form-item label="完结标识" prop="eflag">
          <el-select v-model="form.eflag" placeholder="请选择完结标识">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.glo_status"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="节点编号" prop="nodeno">
          <el-input v-model="form.nodeno" placeholder="请输入节点编号" />
        </el-form-item>
        <el-form-item label="流程编号" prop="nids">
          <el-input v-model="form.nids" placeholder="请输入流程编号" />
        </el-form-item>
        <el-form-item label="描述" prop="memo">
          <el-input v-model="form.memo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSysNode, getSysNode, delSysNode, addSysNode, updateSysNode } from "@/api/system/sysNode";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "SysNode",
  dicts: ['handle_type', 'sys_yes_no', 'glo_status'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 工作流节点表格数据
      sysNodeList: [],
      // 工作流节点树选项
      sysNodeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        wid: undefined,
        pid: undefined,
        name: undefined,
        otype: undefined,
        zflag: undefined,
        ttype: undefined,
        sflag: undefined,
        eflag: undefined,
        status: undefined,
        nodeno: undefined,
        nids: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工作流节点列表 */
    getList() {
      this.loading = true;
      listSysNode(this.queryParams).then(response => {
        this.sysNodeList = this.handleTree(response.data, "nid", "pid");
        this.loading = false;
      });
    },
    /** 转换工作流节点数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.nid,
        label: node.name,
        children: node.children
      };
    },
	/** 查询工作流节点下拉树结构 */
    getTreeselect() {
      listSysNode().then(response => {
        this.sysNodeOptions = [];
        const data = { nid: 0, name: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "nid", "pid");
        this.sysNodeOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        nid: null,
        wid: null,
        cond: null,
        pid: null,
        name: null,
        otype: null,
        zflag: "0",

        ttype: null,
        sort: null,
        sflag: null,
        eflag: null,
        status: null,
        nodeno: null,
        nids: null,
        memo: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.nid) {
        this.form.pid = row.nid;
      } else {
        this.form.pid = 0;
      }
      this.open = true;
      this.title = "添加工作流节点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
	  this.loading = true;
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.pid = row.nid;
      }
      getSysNode(row.nid).then(response => {
	    this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改工作流节点";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
		  this.buttonLoading = true;
          if (this.form.nid != null) {
            updateSysNode(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSysNode(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除工作流节点编号为"' + row.nid + '"的数据项？').then(() => {
        this.loading = true;
        return delSysNode(row.nid);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    }
  }
};
</script>
